<script setup lang="ts">
import { computed } from 'vue';
import { $t } from '@/locales';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';

defineOptions({
  name: 'UserSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const options = [
  {
    label: '进行中',
    value: '进行中'
  },
  {
    label: '点单完成等待上菜',
    value: '点单完成等待上菜'
  },
  {
    label: '已结账',
    value: '已结账'
  }
];

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<Api.Food.OrderSearchParams>('model', { required: true });

type RuleKey = Extract<keyof Api.Food.OrderSearchParams, 'desk' | 'customer' | 'waiter' | 'status' | 'times'>;

const rules = computed<Record<RuleKey, App.Global.FormRule>>(() => {
  const { patternRules } = useFormRules(); // inside computed to make locale reactive

  return {};
});

async function reset() {
  await restoreValidation();
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <NCard :title="$t('common.search')" :bordered="false" size="small" class="card-wrapper">
    <NForm ref="formRef" :model="model" :rules="rules" label-placement="left" :label-width="80">
      <NGrid responsive="screen" item-responsive>
        <NFormItemGi span="24 s:12 m:5" label="桌号" path="desk" class="pr-24px">
          <NInput v-model:value="model.desk" placeholder="桌号" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:5" label="状态" path="status" class="pr-24px">
          <NSelect v-model:value="model.status" placeholder="状态" :options="options" clearable />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:5" label="顾客" path="customer" class="pr-24px">
          <NInput v-model:value="model.customer" placeholder="顾客" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:5" label="服务员" path="waiter" class="pr-24px">
          <NInput v-model:value="model.waiter" placeholder="服务员" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:4" label="时间" path="times" class="pr-24px">
          <NDatePicker v-model:formatted-value="model.times" value-format="yyyy-MM-dd" type="datetime" clearable />
        </NFormItemGi>
        <NFormItemGi span="24 m:12" class="pr-24px">
          <NSpace class="w-full" justify="end">
            <NButton @click="reset">
              <template #icon>
                <icon-ic-round-refresh class="text-icon" />
              </template>
              {{ $t('common.reset') }}
            </NButton>
            <NButton type="primary" ghost @click="search">
              <template #icon>
                <icon-ic-round-search class="text-icon" />
              </template>
              {{ $t('common.search') }}
            </NButton>
          </NSpace>
        </NFormItemGi>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
